package org.bbl.startup;


import org.apache.log4j.Logger;
import org.bbl.common.ProjectResources;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.nio.SelectChannelConnector;
import org.eclipse.jetty.webapp.WebAppContext;

/**
 * @author biniam.gebremichael
 * @since 7/12/12
 */

public class Launch {
    private final Server server;
    private final Logger logger = Logger.getLogger(Launch.class);

    public Launch() {
        server = new Server();
    }

    public void startUp() throws Exception {
        SelectChannelConnector connector = new SelectChannelConnector();
        connector.setPort(8080);
        server.addConnector(connector);

        WebAppContext bb = new WebAppContext();
        bb.setServer(server);
        bb.setContextPath("/");
        bb.setWar(ProjectResources.chainDirs(ProjectResources.getProjectRoot(), "src", "main", "webapp").getAbsolutePath()); //runs from bin dir

        logger.info("WebAppContext=" + bb.toString());
        server.setHandler(bb);
        server.start();
        server.join();
    }

    public void shutdown() {
        try {
            server.stop();
            // System.exit(0);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }


    public static void main(String[] args) throws Exception {
        new Launch().startUp();
    }


}